<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Browse</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
	window.jQuery || document.write('<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery/jquery-2.1.3.js"><\/script>')
</script>
<script type="text/javascript">
	$(document).ready(function(){
		
		$('#categoryView').click(function() {
			$('#advertTable').hide('slow', function() {
				$('#categoryTable').show('slow');
			});
		});
		
		$('#advertView').click(function() {
			$('#categoryTable').hide('slow', function() {
				$('#advertTable').show('slow');
			});
		});
		
	});
</script>
</head>
<body>

<sec:authorize access="isAuthenticated()">
	<c:set var="loggedUserId"><sec:authentication property="principal.id" /></c:set>
</sec:authorize>

<sec:authorize ifAnyGranted="${ advertiserRole }">
	<c:set var="advertiserLoggedIn">true</c:set>
	You're logged as advertiser and you can browse only through your adverts.
</sec:authorize>

<sec:authorize ifAnyGranted="${ agentRole }">
	<c:set var="agentLoggedIn">true</c:set>
</sec:authorize>

<sec:authorize access="isAnonymous()">
	<c:set var="anonymous">true</c:set>
</sec:authorize>

<a id="categoryView" href="#">Category view</a>
<br>
<a id="advertView" href="#">Advert view</a>
<br>

<table id="categoryTable" border="2">
	<thead>
		<tr>
			<td>Category name</td>
			<td>Advert title</td>
		</tr>
	</thead>
	
	<tbody>
		<c:forEach items="${categories}" var="currentCategory">
			<tr>
				<td><a href="${pageContext.request.contextPath}/browse/${currentCategory.name}">${currentCategory.name}</a></td>
				<td>
					<c:forEach items="${currentCategory.adverts}" var="currentAdvert" varStatus="currentAdvertStatus">
						<a href="${pageContext.request.contextPath}/adverts/view/${currentAdvert.id}">${currentAdvert.title}</a>
						<c:if test="${!currentAdvertStatus.last}">
							<c:out value=", "></c:out>
						</c:if>
					</c:forEach>
				</td>
			</tr>
		</c:forEach>
	</tbody>
</table>

<table id="advertTable" border="2" style="display: none" >
	<thead>
		<tr>
			<td>Title</td>
			<c:if test="${ !anonymous }">
				<td>Abstract Text</td>
				<td>Phone Number</td>
				<td>Address</td>
				<td>Advertiser</td>
			</c:if>
			<td>Categories</td>
		</tr>
	</thead>
	
	<tbody>
		<c:forEach items="${adverts}" var="currentAdvert">
			<!-- if user logged in is uploader of advert -->
			<c:if test="${ (advertiserLoggedIn && currentAdvert.advertiser.id == loggedUserId) || (agentLoggedIn) || (anonymous) }">
				<tr>
					<td><a href="${pageContext.request.contextPath}/adverts/view/${currentAdvert.id}">${currentAdvert.title}</a></td>
					<c:if test="${ !anonymous }">
						<td>${currentAdvert.abstractText}</td>
						<td>${currentAdvert.advertiser.phoneNumber}</td>
						<td>${currentAdvert.advertiser.address}</td>
						<td><a href="${pageContext.request.contextPath}/advertisers/${currentAdvert.advertiser.username}">${currentAdvert.advertiser.username}</a></td>
					</c:if>
					<td>
						<c:forEach items="${currentAdvert.categories}" var="currentCategory" varStatus="currentCategoryStatus">
							<a href="${pageContext.request.contextPath}/browse/${currentCategory.name}">${currentCategory.name}</a>
							<c:if test="${!currentCategoryStatus.last}">
								<c:out value=", "></c:out>
							</c:if>
						</c:forEach>
					</td>
				</tr>
			</c:if>
		</c:forEach>
	</tbody>
</table>

</body>
</html>